import requests
from bs4 import BeautifulSoup

user_agent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
headers = {"User-Agent": user_agent}
url = "https://movie.douban.com/top250?start=0&file="

for i in range(0, 10):
    fullUrl = url + str(i * 25)
    res = requests.get(fullUrl, headers=headers)
    soup = BeautifulSoup(res.text, "lxml")
    mList = soup.find_all('div', {'class': 'info'})
    j = 0
    for m in mList:
        j += 1
        title = m.find('a').find('span').text
        score = m.find('div', class_='star').find('span', class_='rating_num').text + '分'
        link = m.find('a')['href']
        content = str((i * 25) + j) + '-' + title + '\n' + score + '\n' + link + '\n'
        with open('D:\\movie250.txt', 'a+') as f:
            f.write(content)
    print("=============第%d页数据存储完毕==============" % (i + 1))
print("==============数据保存完毕==================")
